package pl.edu.pw.MidEmu.Sensing.Values;

import java.util.Random;

/**
 * Class representing luminacy value 
 * 
 * @author Mateusz Starodab
 */
public class LuminancyValue implements IValue
{
	private int changeCount;

	private int trend = 1;

	private String unit = "nit";

	private int value;

	/**
	 * Default class constructor
	 * @param i value
	 * @param string unit
	 */
	public LuminancyValue(int i, String string)
	{
		value = i;
		unit = string;
	}

	/**
	 * Defines how to compare values
	 * @param value Value to compare to
	 * @return -1 when value is lesser, 0 when is equal, ane 1 when is greater
	 */
	@Override
	public int compare(IValue value)
	{
		if (value instanceof LuminancyValue)
		{
			if (this.value > ((LuminancyValue) value).getValue())
				return 1;
			else if (this.value < ((LuminancyValue) value).getValue())
				return -1;
		}
		return 0;
	}

	/**
	 * Decrements value
	 */
	@Override
	public void decrementValue()
	{
		this.value--;
	}

	/**
	 * Gets value unit
	 * @return Value unit
	 */
	@Override
	public String getUnit()
	{
		return unit;
	}

	/**
	 * Gets value as int number
	 * @return Int number representation
	 */
	public int getValue()
	{
		return value;
	}

	/**
	 * Gets value as float number
	 * @return Float number representation
	 */
	@Override
	public float getValueAsFloat()
	{
		return value;
	}

	/**
	 * Gets value as string
	 * @return String representation of value
	 */
	@Override
	public String getValueAsText()
	{
		return toString();
	}

	/**
	 * Increments value
	 */
	@Override
	public void incrementValue()
	{
		this.value++;
	}

	/**
	 * Parses string with value to the IValue format
	 * @param string String with value to parse
	 * @return IValue representation of string
	 */
	@Override
	public IValue parse(String string)
	{
		string = string.trim();
		String[] strArr = string.split(";");
		return new LuminancyValue(Integer.parseInt(strArr[0]), strArr[1]);
	}

	/**
	 * Generates random value
	 */
	@Override
	public void randomValue()
	{
		Random random = new Random(System.currentTimeMillis());
		if (random.nextInt(2) == 1)
		{
			if (trend == 1)
				value += random.nextInt(1) * 3;
			else
				value -= random.nextInt(1) * 3;
		}
		if (changeCount == 10)
		{
			changeCount = 0;
			trend = random.nextInt(2);
		}
		changeCount++;
	}

	/**
	 * Generates random value within specified range
	 * @param lo Low range value
	 * @param hi High range value
	 */
	@Override
	public void randomValue(IValue lo, IValue hi)
	{
		if (lo instanceof LuminancyValue && hi instanceof LuminancyValue)
		{
			int loValue = ((LuminancyValue) lo).getValue();
			int hiValue = ((LuminancyValue) hi).getValue();

			Random random = new Random(System.currentTimeMillis());
			if (random.nextInt(2) == 1)
			{
				if (trend == 1 && value < hiValue - 1)
					value += random.nextInt(10);
				else if (value > loValue + 1)
					value -= random.nextInt(10);
			}
			if (changeCount == 10)
			{
				changeCount = 0;
				trend = random.nextInt(2);
			}
			changeCount++;
		}
	}

	/**
	 * Sets value unit
	 * @param unit Unit to be set for value
	 */
	@Override
	public void setUnit(String unit)
	{
		this.unit = unit;
	}

	/**
	 * Sets value
	 * @param value Value to be set for value
	 */
	public void setValue(int value)
	{
		this.value = value;
	}

	/**
	 * Gets value and unit as string
	 */
	@Override
	public String toString()
	{
		return value + " " + unit;
	}
}
